package com.qf.controller;

import com.qf.pojo.Account;
import com.qf.pojo.Result;
import com.qf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author sunyamei
 * @version V1.0
 * @Project vue_ssm
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/22 15:13
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 添加
     * @param account
     * @return
     */
    @RequestMapping("/addAccount")
    public Result addAccount(@RequestBody Account account){

        try {
            accountService.addAccount(account);
            return  new Result(true,null,"添加账户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,null,"添加账户失败");
        }
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAccount(){
        try {
            List<Account> accountList = accountService.findAll();
            return new Result(true,accountList,"查询所有账户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,"查询所有账户失败");
        }
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @RequestMapping("/{id}")

    public Result deleteById(@PathVariable("id") Integer id){
        try {
            accountService.deleteById(id);
            return new Result(true,null,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,"删除失败");
        }

    }

//    @RequestMapping("/{account}")
////    public  Result updateById(@PathVariable("Account") Account account){
////        try {
////            accountService.updateById(account);
////            return new Result(true,null,"删除成功");
////        } catch (Exception e) {
////            e.printStackTrace();
////            return new Result(false,null,"删除失败");
////        }
////    }

}
